package com.bitnei.battery.examples;

import com.bitnei.battery.bean.processBean.ErrorMessage;
import com.bitnei.battery.bean.processBean.ResultMsg;
import com.bitnei.battery.constants.ResultCode;
import com.bitnei.battery.service.ApiClient;

import java.util.List;

/**
 * @author czl
 * @email czl_nylg@163.com
 * @date 2019/10/14 16:33
 * @Version 1.0
 * @Description: 接口调用示例
 */
public class ApiUtilExample {


    public static void callApi(String url,Object data){

        ApiClient apiClient = ApiClient.getInsatance();
        //token 令牌
        String accessToken = "5cDHGgn6vWIUsgFuCvVSN+9RORSG5FN3";
        // 数据加密KEY
        String dataKey = "Me8BG7XMazxP05Pa";
        // 身份验证KEY
        String signKey = null;

        //初始化服务（首次调用需要初始化）
        apiClient.init(dataKey,signKey,accessToken);

        //调用
        try {
            ResultMsg resultMsg = apiClient.sendApiReq(url, data);
            if(resultMsg.getCode() != ResultCode.OK.getCode())
                System.out.println(resultMsg.getMsg());
            List<ErrorMessage> errMsgList = resultMsg.getErrMsgList();
            if(null == errMsgList || errMsgList.isEmpty()){
                System.out.println("无错误信息");
                return;
            }
            for(ErrorMessage msg : errMsgList){
                System.out.println("类型：" + msg.getType() + "\t顶层标识:" + msg.getTopId() + "\t编码：" + msg.getId() + "\t错误码：" + msg.getCode() + "\t错误信息：" + msg.getMessage() );
            }
            System.out.println("上传完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
